package com.tanhua.controller;

import com.tanhua.service.MovementService;
import com.tanhua.vo.DongTaiVo;
import com.tanhua.vo.MovementVo;
import com.tanhua.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/movements")
public class MovementController {

    @Autowired
    private MovementService movementService;

    @PostMapping
    public ResponseEntity publishMovements(MovementVo movementVo, MultipartFile[] imageContent){
        System.out.println(movementVo);
        System.out.println(imageContent.length);

        movementService.publishMovements(movementVo,imageContent);

        return ResponseEntity.ok(null);
    }

    @GetMapping("/all")
    public ResponseEntity findByUserId(Long userId,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr = movementService.findByUserId(userId,page,pagesize);
        return ResponseEntity.ok(pr);
    }
    @GetMapping
    public ResponseEntity findByFriendId(@RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr = movementService.findByFriendId(page,pagesize);
        return ResponseEntity.ok(pr);
    }

    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr = movementService.recommend(page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查询单条动态
     * @param movementId
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id")String movementId){
        DongTaiVo dongTaiVo = movementService.findByMovementId(movementId);
        return ResponseEntity.ok(dongTaiVo);
    }


    /**
     * 动态点赞
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id")String movementId){
        Integer count = movementService.like(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 取消点赞
     */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id")String movementId){

        Integer count = movementService.dislike(movementId);
        return ResponseEntity.ok(count);
    }

}
